package h0826;

import java.util.LinkedList;
import java.util.Scanner;

/**
  * @description 最长有效括号
  * @author 不知名帅哥
  * @date 2024/8/26 15:36
  * @version 1.0
*/
public class LongestValidBracket {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String s = sc.nextLine();
        int len=getRes(s);
        System.out.println(len);
    }

    private static int getRes(String s) {
        LinkedList<Integer> stack=new LinkedList<>();
        stack.addLast(-1); //初始化栈顶为-1
        int maxLen=0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c=='('){
                stack.addLast(i);
            }else if (c==')'){
                stack.removeLast();
                if (stack.isEmpty()){
                    stack.addLast(i);
                }else {
                    maxLen=Math.max(maxLen,i-stack.peekLast());
                }
            }
        }
        return maxLen;
    }
    private static int getRes2(String s){
        int left = 0, right = 0, maxLength = 0;

        // 从左到右扫描
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(') {
                left++;
            } else {
                right++;
            }

            if (left == right) {
                //更新最大长度
                maxLength = Math.max(maxLength, 2 * right);
            } else if (right > left) {
                left = right = 0;
            }
        }

        left = right = 0;

        // 从右到左扫描
        for (int i = s.length() - 1; i >= 0; i--) {
            if (s.charAt(i) == '(') {
                left++;
            } else {
                right++;
            }

            if (left == right) {
                maxLength = Math.max(maxLength, 2 * left);
            } else if (left > right) {
                left = right = 0;
            }
        }

        return maxLength;
    }
}
